package com.shujia.scala

import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.hbase.{HBaseConfiguration, TableName}
import org.apache.hadoop.hbase.client.{Connection, ConnectionFactory, Get, Result, Table}
import org.apache.hadoop.hbase.util.Bytes
import org.junit.{Before, Test}

class Demo11Hbase {

  var connection: Connection = _

  @Before
  def init(): Unit = {
    //创建配置文件对象
    val configuration: Configuration = HBaseConfiguration.create()
    //指定zk地址
    configuration.set("hbase.zookeeper.quorum", "master,node1,node2")
    //创建链接
    connection = ConnectionFactory.createConnection(configuration)
  }

  @Test
  def get(): Unit = {
    //获取表对象
    val student: Table = connection.getTable(TableName.valueOf("student"))

    //创建get对象
    val get = new Get(Bytes.toBytes("1500100998"))

    //查询数据
    val result: Result = student.get(get)

    val id: String = Bytes.toString(result.getRow)
    //解析数据
    val name: String = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("name")))
    val age: String = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("age")))
    val gender: String = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("gender")))
    val clazz: String = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("clazz")))

    println(s"$id\t$age\t$name\t$gender\t$clazz")

  }
}
